#include <string>
#include "Game.h"
#include <vector>
#include <iostream>
#include <windows.h>
#include "limits.h"
#include <cstdlib>

using namespace std;

Game::Game()
: m_debug(false), m_currentPlayer(0), m_moveCount(0)
{
    //ctor
    // add new game to the vector
}

Game::~Game()
{
    //dtor
}

void Game::init()
{
    setBoard();
    m_currentPlayer = 0;
    m_moveCount = 0;
    countPieces();
    m_stateHistory.push_back(m_board);
    m_debug = false;
}

void Game::displayBoard()
{
	HANDLE  hConsole;
	hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
	cout << endl;
    for(int y = 0; y < static_cast<int>(m_board.size()); y++)
	{
		for(int x = 0; x < static_cast<int>(m_board[y].size()); x++)
		{
			if( m_board[y][x] == NULL )
			{
				SetConsoleTextAttribute(hConsole, 7);
				cout << ". ";
			}
			else
			{
				if(m_board[y][x]->getPlayer() == 0)
					SetConsoleTextAttribute(hConsole, 1);
				else
					SetConsoleTextAttribute(hConsole, 4);
				cout << m_board[y][x]->getType() << ' ';
			}
		}
		cout << endl;
	}
	SetConsoleTextAttribute(hConsole, 7);
	cout << endl << "Player's turn: " << m_currentPlayer << endl;
	cout << "Pieces: " << numPiecesP1 << " " << numPiecesP2 << endl;
}

void Game::retract()
{
    if (m_stateHistory.size() > 1)
    {
        m_stateHistory.pop_back();
        m_moveCount = m_stateHistory.size() - 1;
        m_board = m_stateHistory.back();
        countPieces();
        m_currentPlayer = abs(m_currentPlayer - 1);
    }
    else
    {
        cerr << "Error: No moves to retract" << endl;
    }
}

int Game::evaluate()
{
	if(winningState() != -1)
	{
		if(winningState() == m_currentPlayer)
		{
			int max = 99999;
			return max;
		}
		else
		{
			int min = -99999;
			return min;
		}
	}

	if(m_currentPlayer == 0)
		return numPiecesP1 - numPiecesP2;
	else
		return numPiecesP2 - numPiecesP1;
}

vector<MOVE> Game::legal()
{
	return legal(m_board);
}

void Game::toggleDebug()
{
    m_debug = !m_debug;
}

string Game::getGameName() const
{
    return m_name;
}

void Game::setCurrentPlayer(int player)
{
    m_currentPlayer = player;
}

int Game::getCurrentPlayer() const
{
    return m_currentPlayer;
}

void Game::countPieces()
{
    numPiecesP1 = 0;
    numPiecesP2 = 0;
    for(int y = 0; y < static_cast<int>(m_board.size()); y++)
	{
		for(int x = 0; x < static_cast<int>(m_board[y].size()); x++)
		{
		    if(m_board[y][x] != NULL)
		    {
                if(m_board[y][x]->getPlayer() == 0)
                {
                    numPiecesP1++;
                }
                else if(m_board[y][x]->getPlayer() == 1)
                {
                    numPiecesP2++;
                }
		    }
		}
	}
}

void Game::checkForWinner()
{
    int winner = winningState();
	if(winner != -1)
	{
	    if (winner == 2)
	    {
	        cout << endl <<"### It's a tie! ###" << endl;
	    }
	    else
	    {
            cout << endl << "### Player " << winner << " wins! ###" << endl;
	    }
	}
}

int Game::winningState()
{
    // Check for tie
    if (m_moveCount == 100)
    {
        return 2;
    }
    return -1;
}

bool Game::isTerminal()
{
	if(winningState() != -1)
		return true;
	else
		return false;
}
